<?php
/**
 * 同步disqus评论
 * */

require_once 'config.php';
require_once '../conf/conf.php';
require_once 'class/Mysql.class.php';
require_once 'class/Import.php';

if (!defined('IN_WONDERSHARE')) die('Hacking attempt');
set_time_limit(0);


//note 同步评论数据
function _syn( $db, $site ){
	global $site_config;
	
	$site_api_key = $site_config[$site]['api_key'];

	$im = new Import( $db, $site, $site_api_key );
	$dq = $im->synComment( $site_config[$site]['table'], $site_config[$site]['field_id'] );
	return $dq;
}

//note 取得新评论
function get_new( $db ){
	global $site_config, $site_disqus;
	
	$sid = $_GET['sid'];

	//同步站点的新评论	
	if( isset($site_config[$site_disqus[$sid]]['api_key']) && $site_config[$site_disqus[$sid]]['api_key'] != '' ){
		$dq = _syn( $db, $site_disqus[$sid] );
	}

	$num = $db->getCount('comment', "site = '{$site_disqus[$sid]}' AND article_id > 0 AND is_create = 0");

	echo $num;
}

function del( $db ){
	$sid = $_GET['sid'];
	$cid = $_POST['cid'];
	
	foreach( $cid as $v ){
		$db->delete('comment','cid='.$v);
	}
	echo '<script type="text/javascript">alert("评论删除成功！");document.location.href="list_comment.php?sid='.$sid.'"</script>';	
}

//note 生成文章成功
function create_ok( $db ){
	global $site_disqus;
	
	$sid = $_GET['sid'];
	$d_id = $_GET['d_id'] ? $_GET['d_id'] : 0;
	$db->update('comment', array('is_create'=>1), "d_id IN ({$d_id})");
	
	echo '<script type="text/javascript">parent.synDisqusOk('.$sid.');document.location.href="list_comment.php?sid='.$sid.'"</script>';	
}

$m = $_GET['m'];
$m( new Mysql() );
